<?php

/**
 *
 * Copyright (c) 2011 Tomáš Holub (tomas.holub@webadvice.cz)
 * 
 * @author Tomáš Holub (tomas.holub@webadvice.cz)
 * @package OverFishCMS
 */

namespace AdminModule\Forms;

use Nette\Application\UI\Form;

class GalleryForm extends Form {
    
    public function __construct($parent, $name)
    {
        parent::__construct($parent, $name);

        $this->getElementPrototype()->onsubmit('tinyMCE.triggerSave()');
        $this->addHidden("id");
        $this->addText('title', 'Titulek:')
                ->setRequired('Titulek musí být vyplněn')
                ->setAttribute("class",  "inp-form");
        $this->addCheckbox("active", "Aktivní");
        $this->addSelect("id_menu", "Položka v menu", $this->getMenuChoices());
        $this->addTextArea("content", "Text")
                ->getControlPrototype()->class('mceEditor form-textarea');
        $this->addSubmit('send', 'Odeslat');
        $this->onSuccess[] = array($this, 'formSubmited');
    }
    
    protected function getMenuChoices() {
        $items = $this->getPresenter()->repositories->menuItem->getTreeForSelect();
        
        $result = array("0" => "Žádná");
        
        foreach ($items as $menuItem) {
            $result[$menuItem["title"]] = array();
            
            foreach ($menuItem["items"] as $item) {
                $prefix = "";
                for ($i = 0; $i < $item["level"]; ++$i) {
                    $prefix .= "-";
                }
                $result[$menuItem["title"]][$item["item"]->getId()] = $prefix . $item["item"]->getTitle();
            }
        }
        
        return $result;
    }
    
    public function formSubmited($form)
    {
        if ($form->isValid()) {

            $values = $form->getValues();

            if (!$values->id){

                $gallery = $this->getPresenter()->entities->gallery;
                
                $gallery->setTitle($values->title);
                $gallery->setContent($values->content);
                $gallery->setActive($values->active);
                
                $this->getPresenter()->repositories->gallery->save($gallery);
                
                $this->getPresenter()->repositories->menuItem->assignMenuItem($values->id_menu, $gallery->getId());
                
                $this->getPresenter()->flashMessage('Galerie byla vytvořena.', 'success');
                
                $this->getPresenter()->redirect(':Admin:Gallery:default');
            } else {
                
                $gallery = $this->getPresenter()->repositories->gallery->find($values->id);

                $gallery->setTitle($values->title);
                $gallery->setContent($values->content);
                $gallery->setActive($values->active);
            
                $this->getPresenter()->repositories->gallery->save($gallery);
                
                $this->getPresenter()->repositories->menuItem->assignMenuItem($values->id_menu, $gallery->getId());
                
                $this->getPresenter()->flashMessage('Galerie byla uložena.', 'success');
                
                $this->getPresenter()->redirect(':Admin:Gallery:default');
            }
            
            
            
        }
    }
}

?>
